<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
<!--    <el-form-item label="会员编号" prop="memberId">-->
<!--      <el-input v-model="dataForm.memberId" placeholder="会员编号"></el-input>-->
<!--    </el-form-item>-->
      <el-form-item label="会员编号" prop="memberId">
        <el-select v-model="dataForm.memberId" placeholder="请选择">
          <el-option v-for="(item,i) in memberModel" :label="item.memberNameConcatId" :value="item.memberId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="用药陪护人员编号" prop="drugregisEmployeeid">
        <el-select v-model="dataForm.drugregisEmployeeid" placeholder="请选择">
          <el-option v-for="(item,i) in employeeModel" :label="item.employeeNameConcatId" :value="item.employeeId"></el-option>
        </el-select>
      </el-form-item>
<!--    <el-form-item label="用药陪护人员编号" prop="drugregisEmployeeid">-->
<!--      <el-input v-model="dataForm.drugregisEmployeeid" placeholder="用药陪护人员"></el-input>-->
<!--    </el-form-item>-->
      <el-form-item label="药品名称" prop="drugId">
        <el-select v-model="dataForm.drugId" placeholder="请选择">
          <el-option v-for="(item,i) in drugModel" :label="item.drugName" :value="item.drugId"></el-option>
        </el-select>
      </el-form-item>
    <el-form-item label="药品数量(g)" prop="drugregisNum">
      <el-input v-model="dataForm.drugregisNum" placeholder="药品数量(g) 单位无需填写"></el-input>
    </el-form-item>
      <el-form-item label="用药时间" prop="drugregisTime">
        <el-select v-model="dataForm.drugregisTime" placeholder="请选择">
          <el-option label="早" value="早"></el-option>
          <el-option label="中" value="中"></el-option>
          <el-option label="晚" value="晚"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="用药日期" prop="drugregisDate">
        <el-date-picker
          v-model="dataForm.drugregisDate"
          type="date"
          placeholder="选择日期"
          format="yyyy 年 MM 月 dd 日"
          value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    created () {
      this.getDrug()
      this.getEmployeeInfo()
      this.getMemberInfo()
    },
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          memberId: '',
          drugregisEmployeeid: '',
          drugId: '',
          drugregisNum: '',
          drugregisTime: '',
          drugregisDate: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          memberId: [
            { required: true, message: '会员编号不能为空', trigger: 'blur' }
          ],
          drugregisEmployeeid: [
            { required: true, message: '用药陪护人员不能为空', trigger: 'blur' }
          ],
          drugId: [
            { required: true, message: '药品编号不能为空', trigger: 'blur' }
          ],
          drugregisNum: [
            { required: true, message: '药品数量 克不能为空', trigger: 'blur' }
          ],
          drugregisTime: [
            { required: true, message: '用药时间不能为空', trigger: 'blur' }
          ],
          drugregisDate: [
            { required: true, message: '用药日期不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        },
        drugModel: {
          drugName: '',
          drugId: ''
        },
        employeeModel: {
          employeeId: '',
          employeeNameConcatId: ''
        },
        memberModel: {
          memberId: '',
          memberNameConcatId: ''
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/tb_bs_drugregis/bsdrugregis/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.memberId = data.bsDrugregis.memberId
                this.dataForm.drugregisEmployeeid = data.bsDrugregis.drugregisEmployeeid
                this.dataForm.drugId = data.bsDrugregis.drugId
                this.dataForm.drugregisNum = data.bsDrugregis.drugregisNum
                this.dataForm.drugregisTime = data.bsDrugregis.drugregisTime
                this.dataForm.drugregisDate = data.bsDrugregis.drugregisDate
                this.dataForm.createTime = data.bsDrugregis.createTime
                this.dataForm.updateTime = data.bsDrugregis.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/tb_bs_drugregis/bsdrugregis/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'memberId': this.dataForm.memberId,
                'drugregisEmployeeid': this.dataForm.drugregisEmployeeid,
                'drugId': this.dataForm.drugId,
                'drugregisNum': this.dataForm.drugregisNum,
                'drugregisTime': this.dataForm.drugregisTime,
                'drugregisDate': this.dataForm.drugregisDate,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      getDrug () {
        this.$http({
          url: this.$http.adornUrl(`/tb_bs_drugregis/bsdrugregis/getDrug`),
          method: 'get'
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.drugModel = data.date
          }
        })
      },
      //获取员工信息下拉
      getEmployeeInfo () {
        this.$http({
          url: this.$http.adornUrl(`/tb_bs_protocol/bsprotocol/queryAllEmployee`),
          method: 'get'
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.employeeModel = data.date
          }
        })
      },
      //获取会员信息下拉
      getMemberInfo () {
        this.$http({
          url: this.$http.adornUrl(`/tb_bs_protocol/bsprotocol/queryAllMember`),
          method: 'get'
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.memberModel = data.date
          }
        })
      }
    }
  }
</script>
